package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional(rollbackFor = Exception.class)
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData() {
        String date = LocalDate.now().toString();
        // 获取当天营业额
        String turnover = orderMapper.getTurnoverByDate(date, Orders.COMPLETED);
        if(turnover == null){
            turnover = "0";
        }
        // 获取当天有效订单数
        String validOrderCount = orderMapper.countByDateAndStatus(date, Orders.COMPLETED);
        // 订单完成率：有效订单数 / 总订单数
        Integer totalOrderCount = orderMapper.count(); // 订单总数量
        Integer validOrderCount2 = orderMapper.countByStatus(Orders.COMPLETED); // 订单有效数量
        Double orderCompletionRate = ((double) validOrderCount2 / totalOrderCount);
        // 获取平均客单价
        String totalUser = orderMapper.getTotalUserByDate(date);// 获取当天的总用户量
        Double unitPrice = Double.parseDouble(String.format("%.2f", Double.valueOf(turnover) / Double.valueOf(totalUser))); //保留两位小数
        // 获取新增用户数
        String newUsers = userMapper.getNewUserByDate(date);
        return BusinessDataVO.builder()
                .turnover(Double.valueOf(turnover))
                .validOrderCount(Integer.valueOf(validOrderCount))
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(Integer.valueOf(newUsers))
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        String date = LocalDate.now().toString();
        // 查询当天待接单数量
        Integer waitingOrders = Integer.valueOf(orderMapper.countByDateAndStatus(date, Orders.TO_BE_CONFIRMED));
        // 查询当天待派送数量
        Integer deliveredOrders = Integer.valueOf(orderMapper.countByDateAndStatus(date, Orders.CONFIRMED));
        // 查询当天已完成数量
        Integer completedOrders = Integer.valueOf(orderMapper.countByDateAndStatus(date, Orders.COMPLETED));
        // 查询当天已取消数量
        Integer cancelledOrders = Integer.valueOf(orderMapper.countByDateAndStatus(date, Orders.CANCELLED));
        // 查询当天全部订单
        Integer allOrders = Integer.valueOf(orderMapper.countByDateAndStatus(date, null));
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        // 获取当天已启售数量
        Integer sold = dishMapper.getStatusByDate(StatusConstant.ENABLE);
        // 获取当天已停售数量
        Integer discontinued = dishMapper.getStatusByDate(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        // 获取已启售数量
        Integer sold = setmealMapper.getStatusByDate(StatusConstant.ENABLE);
        // 获取已停售数量
        Integer discontinued = setmealMapper.getStatusByDate(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
